﻿
function ShowPopup(Url, FunctionCallBack, RedirectURL, dxWidth, dxHeight, btncloseid, paraArray, title) {
    var div = "<div id='divDialog'></div>";
    
    Url = GetVirtualPath(Url);

    $(div).load(Url, function (request, reponse, status, xhr) {
        if (request.indexOf("frmLogin") != -1) {
            window.location.href = GetVirtualPath("/Account/Login");
            return;
        }
        if (status == "error") {
            $(this).html(reponse);
        }

        var _width = dxWidth == null ? 'auto' : dxWidth;
        var _height = dxHeight == null ? 'auto' : dxHeight;


        $(this).dialog({
            modal: true,
            height: _height,
            width: _width,
            autoOpen: false,
            resizable: false,
            close: function () {

                $('.ui-dialog').remove();
                $('.container').remove();
                $('.ui-widget-overlay').remove();
                $(this).dialog('destroy').remove();
            },
            draggable: true,
            closeOnEscape: true,
            open: function (event, ui) {
                if (FunctionCallBack && (typeof FunctionCallBack == "function")) {

                    FunctionCallBack(paraArray);
                }
                ClosePopup(btncloseid, RedirectURL);

            }
        });
        $(this).dialog('open');
        if (title != null)
            $('.ui-dialog .ui-dialog-titlebar:last').append(title);
    });
};

function ClosePopup(btncloseid, redirectURL) {
    //btncloseId id cua button close popup
    if (btncloseid != null) {
        $('#' + btncloseid).live('click', function (e) {
            $('#divDialog').remove();
            $('#divDialog').dialog('destroy').remove();
            if (redirectURL != null) {
                window.location = redirectURL;
            }

        })

    }

}

/*=========Enable Validate When Show Dialog */
function EnableValidateForm(formName) {

    var form = $("#" + formName).closest("form");
    form.removeData('validator');
    form.removeData('unobtrusiveValidation');
    $.validator.unobtrusive.parse(form);
}

function ValidateForm(idform) {
    var form = $(idform)
    $.validator.unobtrusive.parse(form);
    var checkValidate = $(form).valid();
    return checkValidate;
}

function GetVirtualPath(path) {
    if (AppPath != '/') {
        return AppPath + path;
    }
    else return path;
}



